﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;

namespace FranceTriple.Dal
{
    public static class ForumInfo
    {
        public static Int32 nbPrivateMessageRead
        {
            get
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    return 0;
                }
            }
        }

        public static Int32 nbPrivateMessageUnread
        {
            get
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    return 0;
                }
            }
        }

        public static Int32 nbAwaitingUsers
        {
            get { return AwaitingUsers.Count; }
        }

        /// <summary>
        /// Liste d'utilisateurs sans role, ou non-approuvé, ou vérouillé.
        /// </summary>
        public static List<MembershipUser> AwaitingUsers
        {
            get
            {
                var users = Membership.GetAllUsers();
                var awaitingUsers = new List<MembershipUser>();

                foreach (MembershipUser user in users)
                {
                    var roles = Roles.GetRolesForUser(user.UserName);
                    // Si l'utilisateur n'a pas de role, ou n'est pas approuvé ou est vérouillé
                    if (roles.Count() == 0 || !user.IsApproved || user.IsLockedOut)
                    {
                        awaitingUsers.Add(user);
                    }
                }
                return awaitingUsers;
            }
        }

        public static Int32 TotalMessage
        {
            get
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    return (from p in ctx.ForumPostSet
                            select p.Id).DefaultIfEmpty().Count();
                }
            }
        }

        public static Int32 TotalUser
        {
            get
            {
                return Membership.GetAllUsers().Count;
            }
        }

        public static String LastCreatedUser
        {
            get
            {
                return (from u in new UserProfile().GetAllMembershipUserArray()
                        where u.CreationDate == new UserProfile().GetAllMembershipUserArray().Max(u1 => u1.CreationDate)
                        select u.UserName).FirstOrDefault();
            }
        }

        public static String CurrentlyConnectedUsernames
        {
            get
            {
                return String.Join(", ", (from u in new UserProfile().GetAllMembershipUserArray()
                                          where u.IsOnline == true
                                          select u.UserName).ToArray());
            }
        }

        public static Int32 TotalCurrentlyConnectedUsers
        {
            get
            {
                return (from u in new UserProfile().GetAllMembershipUserArray()
                        where u.IsOnline == true
                        select u.ProviderUserKey).Count();
            }
        }
    }
}
